#include <SypX/SypX.h>

#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include "MenuScene.h"
#include "MissionScene.h"
#include "MoreScene.h"
#include "SheepDefenceApp.h"
#include "TutorialScene.h"

namespace SheepDefence
{
	MenuScene::MenuScene(SypX::Font* f)
	{
		font = f;
	}

	MenuScene::~MenuScene(void)
	{
		delete bg;
		
		delete play;
		delete challenge;
		delete tutorial;
		delete more;
		
		engine->getAudioEngine()->deleteSoundBuffer(snd);
		
	}

	void MenuScene::init()
	{
		
		engine = SypX::Engine::getSingleton();
		render = engine->getRenderer();
		canvas = engine->getCanvas();	
		if(!font)
		{
			font = engine->loadFontFromFile("Fonts/sheepFont.ttf", 24, 3);
		}

		bgMesh = new SypX::Mesh();
		bgMesh->initAsQuad(320.0f,480.0f);
		
		bgTex = SypX::Texture::loadFromFile("Textures/bgMenu.png");
		
		bg = new SypX::MeshInstance();
		bg->useMesh(bgMesh);
		bg->useTexture(bgTex);

		bg->setPosition(SypX::Vec2(160.0f, 240.0f));	

		render->setClearScreenColor(SypX::Vec4(0.0f, 0.0f,0.0f,1.0f));		
		
		snd = engine->getAudioEngine()->loadFromWAVFile("Sounds/Sega.wav");
		//engine->getAudioEngine()->playSound(snd, SypX::Vec3()) ;

		playTex = SypX::Texture::loadFromFile("Textures/btnPlay.png");
		play = new SypX::GrowingButton(160.0f, 280.0f, 80.0f, 30.0f, 0.9f, playTex);
		play->onUpCallBack = SYPXBIND(&MenuScene::goPlay, this, SYPX_ARG1, SYPX_ARG2);
		buttons.push_back(play);
		
		challengeTex = SypX::Texture::loadFromFile("Textures/btnChallenge.png");
		challenge = new SypX::GrowingButton(160.0f, 220.0f, 80.0f, 30.0f, 0.9f, challengeTex);
		buttons.push_back(challenge); 

		tutorialTex = SypX::Texture::loadFromFile("Textures/btnTutorial.png");
		tutorial = new SypX::GrowingButton(160.0f, 160.0f, 80.0f, 30.0f, 0.9f, tutorialTex);
		tutorial->onUpCallBack = SYPXBIND(&MenuScene::goTutorial, this, SYPX_ARG1, SYPX_ARG2);
		buttons.push_back(tutorial);

		moreTex = SypX::Texture::loadFromFile("Textures/btnMore.png");
		more = new SypX::GrowingButton(160.0f, 100.0f, 80.0f, 30.0f, 0.9f, moreTex);
		more->onDownCallBack = SYPXBIND(&MenuScene::goMore, this, SYPX_ARG1, SYPX_ARG2);
		buttons.push_back(more);
	}

	void MenuScene::handleInput()
	{
		SypX::Event e = canvas->getEvent();

		std::vector<SypX::GrowingButton*>::iterator i = buttons.begin();
		
		if(e.type == SypX::DeviceOrientationLandscapeLeft)
		{
			SypX::ErrorLogger::getLogger()<<"Landscape Left\n";
		}

		while (i != buttons.end())
		{
			if ((*i)->processInput(e))
				break;
			i++;
		}
	}

	void MenuScene::run()
	{
		render->clearScreen();		
		render->setActiveColor(SypX::Vec4(1.0f,1.0f,1.0f,1.0f));
		render->drawMesh(bg);
		handleInput();
		//totalTime += canvas->getElapsedTime();		
		//if(totalTime >= 6.0f)
		//{
		//	changeScene(new MenuScene(font));			
		//}

		play->draw();
		challenge->draw();
		tutorial->draw();
		more->draw();
		
		canvas->swapBuffers();
		//changeScene(new GameScene(font));
	}

	void MenuScene::goPlay(SypX::GuiComponent* g, SypX::Event& e)
	{
		changeScene(new MissionScene(font, 0));
	}

	void MenuScene::goMore(SypX::GuiComponent* g, SypX::Event& e)
	{
		changeScene(new MoreScene(font));
	}

	void MenuScene::goTutorial(SypX::GuiComponent* g, SypX::Event& e)
	{
		changeScene(new TutorialScene(font));
	}

}
